package com.lmiot.xyclick.Bean.SQL;

import android.content.Context;
import com.lmiot.xyclick.Bean.SQL.DaoMaster;
import com.lmiot.xyclick.Bean.SQL.GroupBeanDao;
import com.lmiot.xyclick.Util.TimeUtils;
import java.util.ArrayList;
import java.util.List;
import org.greenrobot.greendao.query.WhereCondition;

public class GroupBeanSqlUtil {
    private static final GroupBeanSqlUtil ourInstance = new GroupBeanSqlUtil();
    private GroupBeanDao mGroupBeanDao;

    public static GroupBeanSqlUtil getInstance() {
        return ourInstance;
    }

    private GroupBeanSqlUtil() {
    }

    public void initDbHelp(Context context) {
        this.mGroupBeanDao = new DaoMaster(new DaoMaster.DevOpenHelper(context, "GroupBean-db", null).getWritableDatabase()).newSession().getGroupBeanDao();
    }

    public void add(GroupBean groupBean) {
        this.mGroupBeanDao.insertOrReplace(groupBean);
    }

    public void addList(List<GroupBean> list) {
        this.mGroupBeanDao.insertOrReplaceInTx(list);
    }

    public void update(GroupBean groupBean) {
        this.mGroupBeanDao.update(groupBean);
    }

    public void updateAll(List<GroupBean> list) {
        try {
            this.mGroupBeanDao.insertOrReplaceInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void delByID(String str) {
        if (str == null) {
            str = "";
        }
        ArrayList arrayList = (ArrayList) this.mGroupBeanDao.queryBuilder().where(GroupBeanDao.Properties.GroupID.eq(str), new WhereCondition[0]).list();
        if (arrayList.size() > 0) {
            this.mGroupBeanDao.delete((GroupBean) arrayList.get(0));
        }
    }

    public void delByName(String str) {
        if (str == null) {
            str = "";
        }
        ArrayList arrayList = (ArrayList) this.mGroupBeanDao.queryBuilder().where(GroupBeanDao.Properties.GroupName.eq(str), new WhereCondition[0]).list();
        if (arrayList.size() > 0) {
            this.mGroupBeanDao.delete((GroupBean) arrayList.get(0));
        }
    }

    public void delAll() {
        try {
            this.mGroupBeanDao.deleteInTx(this.mGroupBeanDao.queryBuilder().build().list());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public GroupBean searchByName(String str) {
        ArrayList arrayList = (ArrayList) this.mGroupBeanDao.queryBuilder().where(GroupBeanDao.Properties.GroupName.eq(str), new WhereCondition[0]).list();
        if (arrayList.size() > 0) {
            return (GroupBean) arrayList.get(0);
        }
        return null;
    }

    public GroupBean searchByID(String str) {
        GroupBeanDao groupBeanDao;
        if (str == null || (groupBeanDao = this.mGroupBeanDao) == null) {
            return null;
        }
        ArrayList arrayList = (ArrayList) groupBeanDao.queryBuilder().where(GroupBeanDao.Properties.GroupID.eq(str), new WhereCondition[0]).list();
        if (arrayList.size() > 0) {
            return (GroupBean) arrayList.get(0);
        }
        return null;
    }

    public List<GroupBean> searchAll() {
        List<GroupBean> list = this.mGroupBeanDao.queryBuilder().orderAsc(GroupBeanDao.Properties.Id).build().list();
        if (list == null) {
            list = new ArrayList<>();
        }
        if (list.size() != 0) {
            return list;
        }
        add(new GroupBean(null, "666", "默认分组", 0, TimeUtils.getCurrentTime(), null));
        return searchAll();
    }
}
